# Create your views here.
#coding:utf-8
from django.http import HttpResponse,HttpResponseRedirect
from django.shortcuts import render
from myApp.models import *
from myApp.forms import *
#from myApp.forms import *
from ctypes import *  #用于.dll文件的调用

from win32com.client import Dispatch #用于MATLAB中.m文件的调用


from django.shortcuts import render_to_response

import pythoncom


def views(request):

    return render_to_response('index.html')

def views2(request):

    return render_to_response('analysis.html')

def views3(request):

    return render_to_response('management.html')

def views4(request):

    return render_to_response('contact.html')



@csrf_exempt
def feedMessage(request):
    if request.POST.has_key("send"):
        userMessageForm=userForm(request.POST)
        if userMessageForm.is_valid():
            userName0 = userMessageForm.cleaned_data['userName']
            userEmail0 = userMessageForm.cleaned_data['userEmail']
            userMessage0 = userMessageForm.cleaned_data['userMessage']
            userFeedMessage.objects.create(userName = userName0,userEmail =userEmail0,userMessage =userMessage0)
            #return render(request,'analysis.html')
            return HttpResponse('您的信息已经发送成功，谢谢您的关注！')
        else:
            return HttpResponse('对不起，您输入的信息有错误！')
    else:
        return render_to_response('contact.html')



@csrf_exempt
def userRegisterFunc(request):
    if request.POST.has_key("add"):
        userRegistForm = userRigit(request.POST)
        if userRegistForm.is_valid():
            xingming0 = str(userRegistForm.cleaned_data['xingming'])
            xingbie0 = str(userRegistForm.cleaned_data['xingbie'])
            nianling0 = int(userRegistForm.cleaned_data['nianling'])
            xueyuan0 = str(userRegistForm.cleaned_data['xueyuan'])
            zhuanye0 = str(userRegistForm.cleaned_data['zhuanye'])
            dizhi0 = str(userRegistForm.cleaned_data['dizhi'])
            youxiang0 = str(userRegistForm.cleaned_data['youxiang'])
            userRegister.objects.create(userName = xingming0,userSex = xingbie0,userAge = nianling0,userAcademy= xueyuan0,
                                        userMajor = zhuanye0,userAddress = dizhi0,userEmail= youxiang0)
            return HttpResponse("添加成功")
        else:
            return HttpResponse("对不起，输入信息有误")
    elif request.POST.has_key("delete"):
        userRegistForm = userRigit(request.POST)
        if userRegistForm.is_valid():
            xingming0 = str(userRegistForm.cleaned_data['xingming'])
            xingbie0 = str(userRegistForm.cleaned_data['xingbie'])
            nianling0 = int(userRegistForm.cleaned_data['nianling'])
            xueyuan0 = str(userRegistForm.cleaned_data['xueyuan'])
            zhuanye0 = str(userRegistForm.cleaned_data['zhuanye'])
            dizhi0 = str(userRegistForm.cleaned_data['dizhi'])
            youxiang0 = str(userRegistForm.cleaned_data['youxiang'])
            delData = userRegister.objects.raw('select * from myApp_userregister o WHERE o.userName = %s',[xingming0])
            for obj in delData:
                obj.delete()
            return HttpResponse('删除信息成功！')
        else:
            return HttpResponse('对给起，您输入信息有误！')
    elif request.POST.has_key("modify"):
        userRegistForm = userRigit(request.POST)
        if userRegistForm.is_valid():
            xingming0 = str(userRegistForm.cleaned_data['xingming'])
            xingbie0 = str(userRegistForm.cleaned_data['xingbie'])
            nianling0 = int(userRegistForm.cleaned_data['nianling'])
            xueyuan0 = str(userRegistForm.cleaned_data['xueyuan'])
            zhuanye0 = str(userRegistForm.cleaned_data['zhuanye'])
            dizhi0 = str(userRegistForm.cleaned_data['dizhi'])
            youxiang0 = str(userRegistForm.cleaned_data['youxiang'])
            modData = userRegister.objects.raw('select * from myApp_userregister o WHERE o.userName = %s',[xingming0])
            for obj in modData:
                obj.delete()
                userRegister.objects.create(userName = xingming0,userSex = xingbie0,userAge = nianling0,userAcademy= xueyuan0,
                                        userMajor = zhuanye0,userAddress = dizhi0,userEmail= youxiang0)
            return HttpResponse("修改信息成功！")
        else:
            return HttpResponse('对给起，您输入信息有误！')
    elif request.POST.has_key("search"):
        userRegistForm = userRigit(request.POST)
        if userRegistForm.is_valid():
            xingming0 = str(userRegistForm.cleaned_data['xingming'])
            xingbie0 = str(userRegistForm.cleaned_data['xingbie'])
            nianling0 = int(userRegistForm.cleaned_data['nianling'])
            xueyuan0 = str(userRegistForm.cleaned_data['xueyuan'])
            zhuanye0 = str(userRegistForm.cleaned_data['zhuanye'])
            dizhi0 = str(userRegistForm.cleaned_data['dizhi'])
            youxiang0 = str(userRegistForm.cleaned_data['youxiang'])
            searchData = userRegister.objects.raw('select * from myApp_userregister o WHERE o.userName = %s',[xingming0])
            i = 0
            strSearchData = ''
            for obj in searchData:
                i= i+1
                strSearchData = strSearchData + '姓名：' + str(obj.userName) + '&nbsp&nbsp&nbsp' +"性别：" +str(obj.userSex)+'<br>'
            strSearchData = '查询结果：' +str(i) +'<br>' + strSearchData
            return HttpResponse(strSearchData)
        else:
            return HttpResponse('对给起，您输入信息有误！')
    elif request.POST.has_key('searchAll'):
            strAllObj0 = '数据库所有用户信息：<br>'
            allObj = userRegister.objects.all()
            strAllObj = ''
            for obj in allObj:
                strAllObj = strAllObj + '姓名：' + str(obj.userName) + '&nbsp&nbsp&nbsp' +"性别：" +str(obj.userSex)+'<br>'
            strAllObj = strAllObj0 + strAllObj
            return HttpResponse(strAllObj)

    #.dll文件的调用
    elif request.POST.has_key("addition"):
        additionvalue=addFuc(request.POST)
        if additionvalue.is_valid():
            val1 = additionvalue.cleaned_data['value1']
            val2 = additionvalue.cleaned_data['value2']
            dll = cdll.LoadLibrary('K:/vs2012_project/Project_python_web1/python_web_test1/Debug/python_web_test1.dll')
            ret =dll.IntAdd(val1,val2)

            return render_to_response('analysis.html',{'current_date': ret})
            #return HttpResponse(str(ret)) #在一个新页面显示ret的值
        else:
            return HttpResponse('对不起，您输入的信息有错误！')

    #MATLAB中.m文件的调用
    elif request.POST.has_key("multiply"):
        multiplyvalue=mulFuc(request.POST)
        if multiplyvalue.is_valid():
            mul_val1 = multiplyvalue.cleaned_data['mul_value1']
            mul_val2 = multiplyvalue.cleaned_data['mul_value2']

            pythoncom.CoInitialize()

            h=Dispatch("Matlab.application")
            h.execute("cd C:/Users/Administrator/Desktop/test3/myApp")

            strExecute = 'multiply(' + str(mul_val1) + ',' + str(mul_val2) + ')'#字符串拼接

            #c = h.execute(strExecute)
            #return HttpResponse(str(c))  #在一个新页面显示ret的值


            c = h.execute(strExecute)

            return render_to_response('analysis.html',{'current_date1': c})

        else:
            return HttpResponse('对不起，您输入的信息有错误.m！')

    else:
        return render_to_response('analysis.html')




#dll文件的调用
#@csrf_exempt
#def additionFunc(request):
#    elif request.POST.has_key("addition"):
#        additionvalue=addFuc(request.POST)
#        if additionvalue.is_valid():
#            val1 = additionvalue.cleaned_data['value1']
#            val2 = additionvalue.cleaned_data['value2']
#            dll = cdll.LoadLibrary('K:/vs2012_project/Project_python_web1/python_web_test1/Debug/python_web_test1.dll')
#            ret =dll.IntAdd(val1,val2)
#
#            return HttpResponse(str(ret))
#        else:
#            return HttpResponse('对不起，您输入的信息有错误！')
#    else:
#        return render_to_response('analysis.html')